package com.example.lowbird.lowbird.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.activity.GoodActivity;
import com.example.lowbird.lowbird.bean.CateRvbean;
import com.example.lowbird.lowbird.bean.CategorylvBean;
import com.example.lowbird.lowbird.bean.HotBean;
import com.example.lowbird.lowbird.constant.Contants;
import com.example.lowbird.lowbird.java.SHopcar;
import com.example.lowbird.lowbird.utils.Animatorutils;

import org.w3c.dom.Text;

import java.util.List;

import static android.R.attr.data;
import static android.R.attr.logo;
import static android.R.attr.numberPickerStyle;
import static android.R.attr.pointerIcon;

/**
 * Created by Administrator on 2016/11/1.
 */
public class CateAdapter extends RecyclerView.Adapter<CateAdapter.Catviewholder> {


    private List<CateRvbean.ListBean> data;
    private Context mContexgt;

    private CateRvbean bean;
    private LayoutInflater inflater;

    private ObjectAnimator obj;

    public CateAdapter(Context mContexgt, CateRvbean bean) {
        this.mContexgt = mContexgt;
        this.bean = bean;
        inflater = LayoutInflater.from(mContexgt);

        data = bean == null ? null : bean.getList();
    }

    @Override
    public Catviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = inflater.inflate(R.layout.adaptercatrv, null);
        return new Catviewholder(inflate);
    }

    @Override
    public void onBindViewHolder(Catviewholder holder, int position) {
        CateRvbean.ListBean listBean = data.get(position);
        Glide.with(mContexgt).load(listBean.getImgUrl()).diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade().into(holder.imageview);
        holder.textview1.setText(listBean.getName());
        holder.textview2.setText("¥ " + listBean.getPrice() + "");




    }


    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void addBean(CateRvbean cateRvbean) {
        if (data == null) {
            data = cateRvbean.getList();
        } else {

            data.addAll(cateRvbean.getList());
        }
        notifyDataSetChanged();
    }

    public void clearhotbean() {
        data = null;
    }

    public void addHotbean(CateRvbean cateRvbean1) {


        if (data == null) {
            data = cateRvbean1.getList();
        } else {

            data.addAll(cateRvbean1.getList());
        }
        Log.e("HOT", "addHotbean: " + data.size());
        notifyDataSetChanged();


    }

    class Catviewholder extends RecyclerView.ViewHolder {
        private ImageView imageview;

        private TextView textview1;

        private TextView textview2;

        public Catviewholder(View itemView) {

            super(itemView);

            imageview = ((ImageView) itemView.findViewById(R.id.iv_cat));
            textview1 = ((TextView) itemView.findViewById(R.id.tv_catname));
            textview2 = ((TextView) itemView.findViewById(R.id.tv_catprice));


            View.OnClickListener lisetenser=new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int layoutPosition = getLayoutPosition();
                    CateRvbean.ListBean listBean = data.get(layoutPosition);
                    SHopcar shopcar=new SHopcar(listBean.getId(),listBean.getImgUrl(),listBean.getName(),
                            1,listBean.getPrice(),15,"1",1);


                    Log.e("CAT", "onClick: " + v.getId()+getLayoutPosition());
                   Animatorutils.initanimation(mContexgt,imageview,shopcar);


                }
            };
            imageview.setOnClickListener(lisetenser);
            textview1.setOnClickListener(lisetenser);

        }

    }


}
